// api/bidApi.ts
import api from './config';

// === 投标文件操作 ===

/**
 * 下载一份投标文件
 */
export const downloadBidFile = async (bidId: string): Promise<void> => {
  const response = await api.get(`/bids/${bidId}/file`, {
    responseType: 'blob', // 关键：接收二进制文件
  });
  const url = window.URL.createObjectURL(new Blob([response.data]));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', `投标文件_${bidId}.xlsx`);
  document.body.appendChild(link);
  link.click();
  link.remove();
  window.URL.revokeObjectURL(url);
};

/**
 * 下载一份二次议价文件
 */
export const downloadBargainingFile = async (bidId: string): Promise<void> => {
  const response = await api.get(`/bids/${bidId}/bargaining/file`, {
    responseType: 'blob',
  });
  const url = window.URL.createObjectURL(new Blob([response.data]));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', `议价文件_${bidId}.xlsx`);
  document.body.appendChild(link);
  link.click();
  link.remove();
  window.URL.revokeObjectURL(url);
};

/**
 * 上传一份二次议价文件
 */
export const uploadBargainingFile = async (bidId: string, file: File): Promise<void> => {
  const formData = new FormData();
  formData.append('file', file);

  await api.post(`/bids/${bidId}/bargaining`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

/**
 * 删除一份二次议价文件
 */
export const deleteBargainingFile = async (bidId: string): Promise<{ message: string }> => {
  const response = await api.delete<{ message: string }>(`/bids/${bidId}/bargaining`);
  return response.data;
};

/**
 * 删除一份投标文件（也会删除其议价文件）
 */
export const deleteBidFile = async (bidId: string): Promise<{ message: string }> => {
  const response = await api.delete<{ message: string }>(`/bids/${bidId}`);
  return response.data;
};